package com.whut.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whut.customer.dao.SysMenuMapper;
import com.whut.customer.entity.SysMenu;
import com.whut.customer.service.ISysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-01-23
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public List<SysMenu> getRoleMenus(Long id) {
        return baseMapper.getRoleMenus(id);
    }

    @Override
    public SysMenu addMenu(SysMenu menu) {
        baseMapper.insert(menu);
        return menu;
    }

    @Override
    public SysMenu updateMenu(SysMenu menu) {
        baseMapper.updateById(menu);
        return menu;
    }

    @Override
    public boolean deleteMenu(SysMenu menu, Long currentUserId) {
        List<SysMenu> removeList = menu.getRemoveMenu();
        if(removeList.size() == 0)
            return false;
        removeList.forEach(item -> {
            //更新相应属性
            item.setUpdateDate(LocalDateTime.now());
            item.setUpdateUserId(currentUserId);
            item.setDelFlag(1);
            baseMapper.updateById(item);
        });

        return true;
    }

    @Override
    public List<SysMenu> selectMenuTree(SysMenu menu) {
        List<SysMenu> list = baseMapper.selectMenuList(menu);
        list.forEach(item -> {
            SysMenu child = new SysMenu();
            child.setMenuParentId(item.getId());
            item.setChildren(selectMenuTree(child));
            BeanUtils.copyProperties(item, child);
        });

        return list;
    }

    @Override
    public List<SysMenu> selectMenuByCondiction(SysMenu menu) {

        return baseMapper.selectMenuList(menu);
    }
}
